/*
 * @Description:
 * @Autor: dongjunhua
 * @Date: 2020-12-21 17:18:58
 * @LastEditors: dongjunhua
 * @LastEditTime: 2021-02-11 14:14:39
 */

import AuthenticationInfo from '../model/AuthenticationInfo';
import AuthorizationInfo from '../model/AuthorizationInfo';
import Account from '@/api/Account';
import User from '@/api/User';

/**
 * safety data source
 * @class Realm
 */
class Realm {
  /**
   * @method doGetAuthenticationInfo
   * @for Realm
   * @param {*} authenticationToken
   * @return {Promise}
   */
  static async doGetAuthenticationInfo(authenticationToken) {
    const { principals, credentials } = authenticationToken;
    if (principals && credentials) {
      // do somethings here
      const res = await Account.login({ principals, credentials });
      return new AuthenticationInfo(res.data[0]);
    }
    // you must return an AuthenticationInfo instance
    return null;
  }

  /**
   * @method doGetAuthorizationInfo
   * @for Realm
   * @param {*} authorizationToken
   * @return {Promise}
   */
  static async doGetAuthorizationInfo(authorizationToken) {
    if (authorizationToken) {
      // do somethings here
      await User.setCurrent({ id: authorizationToken });
      const res = await User.getCurrent();
      return new AuthorizationInfo(res.data[0], 'token');
    }
    // you must return an AuthorizationInfo instance
    return null;
  }

  /**
   * @method beforeLogout
   * @for Realm
   * @return {String} ssoAddress
   */
  static async beforeLogout() {
    // do somethings here if need
    // e.g., http request such as logout api
    // you may return a ssoAddress when login type using sso
    return;
  }
}

export default Realm;
